using DevExpress.XtraEditors;
using DXApplication_TestDemo.Models.ProgramModel;
using DXApplication_TestDemo.ViewModels;
using System;
using System.Linq;

namespace DXApplication_TestDemo.Views
{
    public partial class MainView : XtraForm
    {
        public MainView()
        {
            InitializeComponent();

            //使用单例vm保证数据一致性
            mvvmContext1.SetViewModel(typeof(MainViewModel), ProgramModel.Instance.MainViewModels);

            InitializeBindings();

            simpleButton2.Click += (s, e) =>
            {
                MeasView measView = new MeasView();
                measView.Show();
            };

        }

        // 初始化绑定
        void InitializeBindings()
        {
            var fluent = mvvmContext1.OfType<MainViewModel>();

            //绑定方法一
            fluent.SetBinding(labelControl1, label => label.Text, vm => vm.MainModels.Age);

            //绑定方法二
            var viewModel = mvvmContext1.GetViewModel<MainViewModel>();
            //var viewModel = mvvmContext1.OfType<MainViewModel>().ViewModel;

            //labelControl1.DataBindings.Add("Text", viewModel.MainModels, "Age", true);
            textEdit1.DataBindings.Add("Text", viewModel.MainModels, "Age2", true);

            //combox集合绑定
            //comboBox1.DataBindings.Add(nameof(comboBox1.DataSource), viewModel, nameof(viewModel.StrColl));
            //comboBox1.DataBindings.Add(nameof(comboBox1.SelectedValue), viewModel, nameof(viewModel.StrSel), false, DataSourceUpdateMode.OnPropertyChanged);
            //comboBox1.DisplayMember = "Column1";  //显示集合对象中的属性
            //comboBox1.ValueMember = "Column2";    //选择集合对象中的属性

            //绑定集合
            //comboBox1_Fruit.DataSource = viewModel.MainModels.FruitColl;
            comboBox1_Fruit.Items.AddRange(viewModel.MainModels.FruitColl.ToArray());
            comboBox1_Fruit.DataBindings.Add("SelectedItem", viewModel.MainModels, "FruitSel", true);

            //绑定按钮
            simpleButton1.BindCommand(viewModel.SelectDataCommand);

            //触发器
            fluent.SetTrigger(x => x.MainModels.Age2, (x) =>
            {
                if (x == 123)
                {
                    XtraMessageBox.Show($"{x}");
                }
            });

        }

        int i = 1;
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            //SettingView settingView = new SettingView();
            //settingView.Show();

            var viewModel = mvvmContext1.GetViewModel<MainViewModel>();
            if (i < viewModel.MainModels.FruitColl.Count)
                viewModel.MainModels.FruitSel = viewModel.MainModels.FruitColl[i++];
            else
                i = 0;
        }

        
        private void simpleButton2_Click(object sender, EventArgs e)
        {
            //MeasView measView = new MeasView();
            //measView.Show();

         
        }
    }
}
